#include "irm_objectrequest.h"
#include "irm_types.h"
#include <QDebug>
#include <QNetworkAccessManager>


namespace ophies {

	ObjectRequest::ObjectRequest(
		QString controllerName,
		RequestCallback *callback,
		QObject *parent) :
	QObject(parent),
		m_version("v1"),
		m_controllerName(controllerName),
		m_search(NULL),
		m_request(new QNetworkRequest()),
		m_requestCallback(callback),
		//    m_cookieJar(new QNetworkCookieJar()),
		m_manager(new QNetworkAccessManager(this))

	{
		connect(m_manager, SIGNAL(finished(QNetworkReply*)),
			m_requestCallback, SLOT(onFinished(QNetworkReply*)));
		connect(m_manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
			m_requestCallback, SLOT(onAuthenticationRequired(QNetworkReply*,QAuthenticator*)));

		//    m_manager->setCookieJar(m_cookieJar);
		this->setAuth(USER_NAME, PASSWORD);
	}

	ObjectRequest::ObjectRequest(
		QString controllerName,
		RequestCallback *callback,
		Search* searchOptions,
		QObject *parent) :
	QObject(parent),
		m_version("v1"),
		m_controllerName(controllerName),
		m_search(searchOptions),
		m_request(new QNetworkRequest()),
		m_requestCallback(callback),
		m_manager(new QNetworkAccessManager(parent))

	{
		connect(m_manager, SIGNAL(finished(QNetworkReply*)),
			m_requestCallback, SLOT(onFinished(QNetworkReply*)));
		connect(m_manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
			m_requestCallback, SLOT(onAuthenticationRequired(QNetworkReply*,QAuthenticator*)));

		this->setAuth(USER_NAME, PASSWORD);
	}


	ObjectRequest::~ObjectRequest()
	{
		std::cout << "ObjectRequest: " << m_controllerName.toStdString() << " destroyed" << std::endl;
		//delete m_params; // param is supposed to be stack variable
		delete m_request;
		delete m_manager;
	}

	QString ObjectRequest::buildPath(QString id, QString action)
	{
		QString path = QString("%1/%2").arg(m_version, m_controllerName);
		if(id.length() != 0)
			path += "/" + id;
		if(action.length() != 0)
			path += "/" + action;


		path += ".json";

		return path;
	}

	void ObjectRequest::setAuth(QString tokenId, QString secret)
	{
		QByteArray auth;
		QString cred = tokenId.append(QString(":"));
		cred.append(secret);

		QByteArray b = cred.toLocal8Bit().toBase64();

		auth.append(QString("Basic "));
		auth.append(QString(b).toLocal8Bit());
		m_request->setRawHeader("Authorization", auth);
	}

	void ObjectRequest::listObject()
	{
		QString address = QString(URL) + "/" + buildPath();
		QUrl url(address);

		if(m_search)
		{
			m_search->setQueryTo(url);
		}

		m_request->setUrl(url);

		std::cout << m_request->url().toString().toStdString() << std::endl;
		int b = m_manager->networkAccessible();
		std::cout << "network accesible (supposed to be 1): " << b << std::endl;

		m_manager->get(*m_request);	
	}


	void ObjectRequest::getObject(QString id)
	{
		QString address = QString(URL) + "/" + buildPath(id, "");
		QUrl url(address);

		qDebug() << "get object id: " + id;

		if(m_search)
		{
			m_search->setQueryTo(url);
			qDebug() << "query string = " << m_search->toQueryString();
		}

		m_request->setUrl(url);

		std::cout << m_request->url().toString().toStdString() << std::endl;
		m_manager->get(*m_request);
	}

}
